﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DWF.Studio.ScriptEngine.Event;
using DWF.Studio.ScriptEngine.IPC.Msg;
using DWF.Studio.ScriptEngine.IPC.WorkFlow;
using DWF.Studio.Share.Serialization;

namespace DWF.Studio.ScriptEngine.Proxy
{
    internal class WorkflowScriptEngineProxy : ScriptEngineBase
    {
        private ScriptEngineProxyContext _engineContext = null;

        public WorkflowScriptEngineProxy(ScriptEngineProxyContext engineContext)
        {
            _engineContext = engineContext;
            _engineContext.Push(this);
        }

        public WorkflowScriptEngineProxy(string id, ScriptEngineProxyContext engineContext)
            : this(engineContext)
        {
            Id = id;
        }

        public override void Cancel()
        {
            sendDownwardMsg<string>(String.Empty, EMsgType.CancelScript);
        }

        public override void DebugScript(string projectDirectory, string scriptPath)
        {
            DebugScript(projectDirectory, scriptPath, null);
        }

        public override void DebugScript(string projectDirectory, string scriptPath, Dictionary<string, object> param)
        {
            string[] args = new string[3];
            args[0] = projectDirectory;
            args[1] = scriptPath;
            if (param != null)
            {
                args[2] = JsonHelper.SerializeWithTypeName(param);
            }

            sendDownwardMsg<string[]>(args, EMsgType.DebugScript);
        }

        public override void ExecuteScript(string projectDirectory, string scriptPath)
        {
            ExecuteScript(projectDirectory, scriptPath, null);
        }

        public override void ExecuteScript(string projectDirectory, string scriptPath, Dictionary<string, object> param)
        {
            string[] args = new string[3];
            args[0] = projectDirectory;
            args[1] = scriptPath;
            if (param != null)
            {
                args[2] = JsonHelper.SerializeWithTypeName(param);
            }

            sendDownwardMsg<string[]>(args, EMsgType.ExcuteScript);
        }

        public override void CallFrameInfo()
        {
            sendDownwardMsg<string>("Msg_Debug_CallFrame", EMsgType.DebugTrack);
        }

        protected override void OnDisposing()
        {
            _engineContext = null;

            this.ExecuteAborted = null;
            this.ExecuteCompleted = null;
            this.ExecuteOnUnhandledException = null;
            this.ExecuteTrack = null;
            this.ExcuteOutput = null;
            this.ExecuteStart = null;
            this.ExecuteEnd = null;
        }

        private void sendDownwardMsg<T>(T datum, EMsgType msgType)
        {
            _engineContext.SendMsg(datum, msgType, this.Id, EEngineType.WorkflowScriptEngine);
        }

        public override void DebugResume()
        {
            sendDownwardMsg<string>("Msg_Debug_Resume", EMsgType.DebugTrack);
        }

        public override void DebugStepInto()
        {
            sendDownwardMsg<string>("Msg_Debug_StepInto", EMsgType.DebugTrack);
        }

        public override void DebugStepOut()
        {
            sendDownwardMsg<string>("Msg_Debug_StepOut", EMsgType.DebugTrack);
        }

        public override void DebugStepOver()
        {
            sendDownwardMsg<string>("Msg_Debug_StepOver", EMsgType.DebugTrack);
        }
    }
}
